package transfer;

import java.util.ArrayList;

/**
 * Represents user role
 *
 * @author Oleh
 */
public class UserRole implements Comparable {

    public static final Integer ID_CUSTOMER = new Integer(1);
    public static final Integer ID_ADMIN = new Integer(2);
    public static final Integer ID_WAREHOUSE_EMPLOYEE = new Integer(3);
    public static final Integer ID_COURIER = new Integer(4);
    public static final String NAME_CUSTOMER = "customer";
    public static final String NAME_ADMIN = "admin";
    public static final String NAME_WAREHOUSE_EMPLOYEE = "warehouse_employee";
    public static final String NAME_COURIER = "courier";
    public static final UserRole USER = new UserRole(ID_CUSTOMER, NAME_CUSTOMER);
    public static final UserRole ADMIN = new UserRole(ID_ADMIN, NAME_ADMIN);
    public static final UserRole WAREHOUSE_EMPLOYEE = new UserRole(ID_WAREHOUSE_EMPLOYEE, NAME_WAREHOUSE_EMPLOYEE);
    public static final UserRole COURIER = new UserRole(ID_COURIER, NAME_COURIER);
    public static final UserRole ALL = new UserRole(new Integer(0), "all");
    private Integer idRole;
    private String nameRole;

    public UserRole() {
    }

    ;     
    
    private UserRole(Integer idRole, String nameRole) {
        this.idRole = idRole;
        this.nameRole = nameRole;
    }

    /**
     *
     * @param idRole
     * @return instance id UserRole
     */
    public static UserRole instance(Integer idRole) {
        if (idRole == null) {
            return null;
        }
        switch (idRole.intValue()) {
            case 0:
                return ALL;
            case 1:
                return USER;
            case 2:
                return ADMIN;
            case 3:
                return WAREHOUSE_EMPLOYEE;
            case 4:
                return COURIER;
            default:
                return null;
        }
    }

    public Integer getIdRole() {
        return idRole;
    }

    public String getNameRole() {
        return nameRole;
    }

    public int hashCode() {
        return idRole.intValue();
    }

    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final UserRole other = (UserRole) obj;
        return idRole.equals(other.idRole);
    }

    /**
     * This method for sort non customer role. In sorted list the first role has
     * hight priority
     *
     * @param obj
     * @return
     */
    public int compareTo(Object obj) {
        final UserRole other = (UserRole) obj;
        return hashCode() - other.hashCode();
    }
    
    public static ArrayList getAllRoles() {
        ArrayList roles = new ArrayList();
        for (int i = ID_CUSTOMER.intValue(); i <= ID_COURIER.intValue(); i++) {
            roles.add(instance(new Integer(i)));
        }
        return roles;
    }
}
